# SPDX-License-Identifier: Apache-2.0
"""
Add Vulnerability and release association tables

Revision ID: 1dbb95161e5a
Revises: 08ccc59d9857
Create Date: 2021-07-14 19:01:16.356312
"""

import sqlalchemy as sa

from alembic import op
from sqlalchemy.dialects import postgresql

revision = "1dbb95161e5a"
down_revision = "08ccc59d9857"


def upgrade():
    op.create_table(
        "vulnerabilities",
        sa.Column("source", sa.String(), nullable=False),
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("link", sa.String(), nullable=True),
        sa.Column("aliases", postgresql.ARRAY(sa.String()), nullable=True),
        sa.PrimaryKeyConstraint("source", "id"),
    )
    op.create_table(
        "release_vulnerabilities",
        sa.Column("release_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("vulnerability_source", sa.String(), nullable=False),
        sa.Column("vulnerability_id", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["release_id"], ["releases.id"], onupdate="CASCADE", ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(
            ["vulnerability_source", "vulnerability_id"],
            ["vulnerabilities.source", "vulnerabilities.id"],
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
    )


def downgrade():
    op.drop_table("release_vulnerabilities")
    op.drop_table("vulnerabilities")
